//
//  ByViewDetailParams.m
//  itboye_shop_ios
//
//  Created by pro on 17/2/10.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewDetailParams.h"

#define KMarginLeft     10
#define KMarginCenter   40
#define KTitleWidth     ([@"这是五个字" getSizeOfString:14].width + 5)
#define KDetailWidth    (SCREEN_WIDTH - KMarginLeft * 2 - KMarginCenter - KTitleWidth)

@implementation ByViewDetailParams

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame style:UITableViewStyleGrouped]) {
        
        [self setupDefault];
    }
    return self;
}

- (void)setupDefault {
    
    self.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1];
    self.delegate = self;
    self.dataSource = self;
    
    self.separatorStyle = UITableViewCellSelectionStyleNone;
    
    self.allowsSelection = NO;
    
}

#pragma mark - 创建视图
- (void)setupCell:(UITableViewCell *)cell {
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    CGFloat x = KMarginLeft;
    CGFloat y = KMarginLeft;
    CGFloat width = KTitleWidth;
    CGFloat height = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.numberOfLines = 0;
    titleLabel.font = FONT(14);
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:titleLabel];
    
    x = titleLabel.right + KMarginCenter;
    y = titleLabel.top;
    width = KDetailWidth;
    height = 20;
    UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    detailLabel.numberOfLines = 0;
    detailLabel.font = FONT(14);
    detailLabel.textColor = [UIColor blackColor];
    detailLabel.textAlignment = NSTextAlignmentLeft;
    [cell.contentView addSubview:detailLabel];
    
    x = 0;
    y = 0;
    width = SCREEN_WIDTH;
    height = 0.5;
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    line.backgroundColor = BY_LINE_COLOR;
    [cell.contentView addSubview:line];
    
    // 设置tag
    titleLabel.tag = 100;
    detailLabel.tag = 200;
    line.tag = 300;

}
- (void)setupData:(UITableViewCell *)cell paramDic:(NSDictionary *)dic {
    
    UILabel *titleLabel = [cell.contentView viewWithTag:100];
    UILabel *detailLabel = [cell.contentView viewWithTag:200];
    UILabel *line = [cell.contentView viewWithTag:300];

    CGFloat height;
    titleLabel.text = dic[@"title"];
    height = [titleLabel.text getRectOfStringWithSize:CGSizeMake(titleLabel.width, 10000) font:14].size.height;
    titleLabel.height = MAX(20, height);
    
    detailLabel.text = dic[@"detail"];
    height = [detailLabel.text getRectOfStringWithSize:CGSizeMake(detailLabel.width, 10000) font:14].size.height;
    detailLabel.height = MAX(20, height);
    
    cell.height = MAX(titleLabel.height, detailLabel.height) + titleLabel.top * 2;
    
    line.bottom = cell.height;
    
}

#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.params.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        
        // 创建视图
        [self setupCell:cell];
    }
    
    // 设置属性
    [self setupData:cell paramDic:self.params[indexPath.row]];
    
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat height1, height2;
    NSDictionary *dic = self.params[indexPath.row];
    height1 = [dic[@"title"] getRectOfStringWithSize:CGSizeMake(KTitleWidth, 10000) font:14].size.height;
    
    height2 = [dic[@"detail"] getRectOfStringWithSize:CGSizeMake(KDetailWidth, 10000) font:14].size.height;
    
    return MAX(MAX(height1, 20), MAX(height2, 20)) + KMarginLeft * 2;
    
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}

#pragma mark - 滑动视图代理
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
    if (self.scrollViewDidEndDragging) {
        self.scrollViewDidEndDragging(scrollView, decelerate);
    }
}

@end
